/**
 * Created by xie on 2018/3/31.
 */

angular.module('module.mainpage')
    .controller('AddPlantController', AddPlantController);

AddPlantController.$inject = ['$scope','$state','$cookies','$rootScope','FileUpload','MainpageService'];

function AddPlantController($scope, $state,$cookies, $rootScope,FileUpload,MainpageService) {
    if(!$cookies.get("userInfo")){
        $state.go("login");
    }
    var vm = this;
    vm.selectlabel = selectlabel;
    vm.add = false;
    vm.addpicurl="images/mainpage/camera.png";
    vm.addpicstyle="addpiccamera";
    vm.sort = "请选择标签";
    vm.mymodelfont="lightgray;";
    vm.uploadLogo=uploadLogo;//上传图片文件
    vm.addArticle=addArticle;
    vm.user = JSON.parse($cookies.get("userInfo"));

    vm.article={
        "articleid": 0,
        "articlename": "",
        "articlepic": "",
        "userid": 0,
        "createtime": "",
        "articlecontent": "",
        "articletype": ""
    }
    vm.select = [
        "幽静唯美",
        "花中萌物",
        "奇花异草",
        "网友随拍",
        "植物与人",
        "求鉴定" ,
    ]

    function selectlabel(check) {
        vm.sort = check;
        vm.article.articletype=check;
        vm.mymodelfont="blue;";
    }



    function uploadLogo(file) {
        if (!file) {
            return;
        }
        FileUpload.uploadFileToUrl1(file,file.name).then(function (data) {
            vm.addpicurl = data.data.faddress;
            vm.article.articlepic=data.data.faddress;
            vm.addpicstyle="addpicimg";
        }, function (error) {
            console.log(error)
        })
    }



    function addArticle(){
        vm.article.articlename=vm.article.articlecontent.substring(0,16);
        vm.article.userid=vm.user.userid;
        console.log("at",vm.article);

        MainpageService.addArticle(vm.article).$promise.then(function (data) {
            request = data;
            console.log(request)
            alert("新增成功");
            $state.go('mainpage');
        }, function (err) {
            console.log(err);
            return alert(err.data.msg);
        });
    }

}
